package dljk.images;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public final class ResourcesLoader {
	
	private static final ResourcesLoader instance;
	private final static Set<String> DEFAULT_lOCATIONS;
	static {
		DEFAULT_lOCATIONS = new HashSet<String>();
		DEFAULT_lOCATIONS.add("defaultCardSet");
		instance = new ResourcesLoader();
	}

	private ResourcesLoader() {
		this(DEFAULT_lOCATIONS);
	}
	
	private ResourcesLoader(Set<String> locations) {
		
		this.locations = locations;
	}
	
	public static ResourcesLoader instance() {
		return instance;
	}
	
	public URL location(String name,String suitSet) throws ResourcesLoaderException {

		URL resourceLocation = null;
		resourceLocation = ResourcesLoader.class.getResource(suitSet + "/" + name);

		
		if (resourceLocation == null) {
			
			String resourceLocations = "";
			for (String location : locations) {
				resourceLocations += "Direcory with class: " + ResourcesLoader.class.getName() + " / " + location + "\n";
			}
			throw new ResourcesLoaderException(String.format("Resource %s was not found in %s.\nAll resources should be in \n %s", name, suitSet, resourceLocations));
		}
		
		return resourceLocation;

	}
        
        public BufferedImage getCardImage(String cardName){
            return getCardImage(cardName,"defaultCardSet");
        }
        
        public BufferedImage getCardImage(String cardName,String suitSet){
        BufferedImage img = null;
        try {
            try {
                img=ImageIO.read(ResourcesLoader.instance().location(cardName,suitSet));
            } catch (IOException ex) {
                Logger.getLogger(ResourcesLoader.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (ResourcesLoaderException ex) {
            Logger.getLogger(ResourcesLoader.class.getName()).log(Level.SEVERE, null, ex);
        }
        return img;
        }
	
	private final Set<String> locations;
	
}
